<!DOCTYPE html>



  


<html class="theme-next pisces use-motion" lang="">
<head>
  <meta charset="UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"/>
<meta name="theme-color" content="#222">









<meta http-equiv="Cache-Control" content="no-transform" />
<meta http-equiv="Cache-Control" content="no-siteapp" />
















  
  
  <link href="/blog/lib/fancybox/source/jquery.fancybox.css?v=2.1.5" rel="stylesheet" type="text/css" />







<link href="/blog/lib/font-awesome/css/font-awesome.min.css?v=4.6.2" rel="stylesheet" type="text/css" />

<link href="/blog/css/main.css?v=5.1.4" rel="stylesheet" type="text/css" />


  <link rel="apple-touch-icon" sizes="180x180" href="/blog/images/apple-touch-icon-next.png?v=5.1.4">


  <link rel="icon" type="image/png" sizes="32x32" href="/blog/images/favicon-32x32-next.png?v=5.1.4">


  <link rel="icon" type="image/png" sizes="16x16" href="/blog/images/favicon-16x16-next.png?v=5.1.4">


  <link rel="mask-icon" href="/blog/images/logo.svg?v=5.1.4" color="#222">





  <meta name="keywords" content="Hexo, NexT" />










<meta name="description" content="一、NSDictionary  NSArray和NSMutableArray数组 存储数据特点:每个元素紧密相连，并且每个元素中都是直接存储的值. 缺点:数组元素下标不固定，都有可能发生变化，无法通过下标来唯一确定数组中的元素.">
<meta name="keywords" content="Android、iOS、Web、React Native">
<meta property="og:type" content="article">
<meta property="og:title" content="Objective-C-学习第十天">
<meta property="og:url" content="http://yoursite.com/blog/2018/08/06/Object-C/Objective-C-学习第十天/index.html">
<meta property="og:site_name" content="凌浩雨_博客">
<meta property="og:description" content="一、NSDictionary  NSArray和NSMutableArray数组 存储数据特点:每个元素紧密相连，并且每个元素中都是直接存储的值. 缺点:数组元素下标不固定，都有可能发生变化，无法通过下标来唯一确定数组中的元素.">
<meta property="og:locale" content="default">
<meta property="og:updated_time" content="2018-08-06T10:34:35.537Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="Objective-C-学习第十天">
<meta name="twitter:description" content="一、NSDictionary  NSArray和NSMutableArray数组 存储数据特点:每个元素紧密相连，并且每个元素中都是直接存储的值. 缺点:数组元素下标不固定，都有可能发生变化，无法通过下标来唯一确定数组中的元素.">



<script type="text/javascript" id="hexo.configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '/blog/',
    scheme: 'Pisces',
    version: '5.1.4',
    sidebar: {"position":"left","display":"post","offset":12,"b2t":false,"scrollpercent":false,"onmobile":false},
    fancybox: true,
    tabs: true,
    motion: {"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},
    duoshuo: {
      userId: '0',
      author: 'Author'
    },
    algolia: {
      applicationID: '',
      apiKey: '',
      indexName: '',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    }
  };
</script>



  <link rel="canonical" href="http://yoursite.com/blog/2018/08/06/Object-C/Objective-C-学习第十天/"/>





  <title>Objective-C-学习第十天 | 凌浩雨_博客</title>
  








</head>

<body itemscope itemtype="http://schema.org/WebPage" lang="default">

  
  
    
  

  <div class="container sidebar-position-left page-post-detail">
    <div class="headband"></div>

    <header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-wrapper">
  <div class="site-meta ">
    

    <div class="custom-logo-site-title">
      <a href="/blog/"  class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">凌浩雨_博客</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
      
        <p class="site-subtitle"></p>
      
  </div>

  <div class="site-nav-toggle">
    <button>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
    </button>
  </div>
</div>

<nav class="site-nav">
  

  
    <ul id="menu" class="menu">
      
        
        <li class="menu-item menu-item-home">
          <a href="/blog/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-home"></i> <br />
            
            Home
          </a>
        </li>
      
        
        <li class="menu-item menu-item-archives">
          <a href="/blog/archives/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-archive"></i> <br />
            
            Archives
          </a>
        </li>
      

      
    </ul>
  

  
</nav>



 </div>
    </header>

    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
    

  

  
  
  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="http://yoursite.com/blog/blog/2018/08/06/Object-C/Objective-C-学习第十天/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="凌浩雨">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="/blog/images/avatar.jpeg">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="凌浩雨_博客">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">Objective-C-学习第十天</h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">Posted on</span>
              
              <time title="Post created" itemprop="dateCreated datePublished" datetime="2018-08-06T18:01:39+08:00">
                2018-08-06
              </time>
            

            

            
          </span>

          

          
            
          

          
          

          

          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        <p>一、NSDictionary</p>
<ol>
<li>NSArray和NSMutableArray数组<br> 存储数据特点:每个元素紧密相连，并且每个元素中都是直接存储的值.<br> 缺点:数组元素下标不固定，都有可能发生变化，无法通过下标来唯一确定数组中的元素.</li>
</ol>
<a id="more"></a>
<ol start="2">
<li><p>NSDictionary(Key-Value存储)与NSMutableDictionary<br> 1). 它们是数组，以键值对的形式存储数据，通过别名来找数据，类似于java中的Map集合.<br> 2). 存储数据原理</p>
<pre><code>a. 以键值对的形式存储数据
b. 字典数组一旦创建，其中的元素就无法动态的新增和删除
c. 键: 只能是遵守了NSCoping协议的对象，而NSString遵守了这个协议
值：只能是OC对象
</code></pre><p> 3). 创建字典数组</p>
 <figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">NSDictionary *dict1 = [NSDictionary new];</span><br><span class="line">NSDictionary *dict2 = [[NSDictionary alloc] init];</span><br><span class="line">NSDictionary *dict3 = [NSDictionary dictionary];</span><br></pre></td></tr></table></figure>
<p> 这种方式创建出来的字典数组没有任何元素，所以没有意义.<br> 4). 一般创建方式<br> + (instancetype)dictionaryWithObjectsAndKeys:(id)firstObject…</p>
 <figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:@&quot;jack&quot;, @&quot;name&quot;,nil];</span><br></pre></td></tr></table></figure>
<p> 将字典数组的值键依次写在后面初始化.<br> 5). 简要创建方式<br> @{键1:值1,键2:值2…}</p>
 <figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">NSDictionary *dict = @&#123;@&quot;name&quot;:@&quot;rose&quot;,@&quot;age&quot;:@&quot;18&quot;&#125;;</span><br></pre></td></tr></table></figure>
<p> 6). 使用%@可以打印这个数组中所有的元素.</p>
<pre><code>如果字典中没有键名，则返回nil.
1). 别名法
    对象名[键名];
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">NSDictionary *dict = @&#123;@&quot;name&quot;:@&quot;rose&quot;,@&quot;age&quot;:@&quot;18&quot;&#125;;</span><br><span class="line">dict[@&quot;name&quot;];</span><br></pre></td></tr></table></figure>

2). 方法
- (id)objectForKey:(id)aKey;
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">NSDictionary *dict = @&#123;@&quot;name&quot;:@&quot;rose&quot;,@&quot;age&quot;:@&quot;18&quot;&#125;;</span><br><span class="line">[dict objectForKey:@&quot;name&quot;];</span><br></pre></td></tr></table></figure>

3). 取出键值对的个数
@property (readonly)NSUInteger count;
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">NSDictionary *dict = @&#123;@&quot;name&quot;:@&quot;rose&quot;,@&quot;age&quot;:@&quot;18&quot;&#125;;</span><br><span class="line">[dict count];</span><br></pre></td></tr></table></figure>
</code></pre></li>
</ol>
<ol start="3">
<li><p>遍历NSDictionary<br> 1). 字典数组中的数据无法使用下标取<br> 2). 使用for in 循环，遍历出来的是所有数组的键.</p>
 <figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">NSDictionary *dict = @&#123;@&quot;name&quot;:@&quot;rose&quot;,@&quot;age&quot;:@&quot;18&quot;&#125;;</span><br><span class="line">for(NSString *key : dict)&#123;</span><br><span class="line">	NSLog(@&quot;%@ = %@&quot;, key, dict[key]);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p> 3). 使用block遍历<br> [dict enumerateKeysAndObjectsUsingBlock:^(id _Nonnull key, id _Nonnull obj, BOOL * _Nonnull stop)]{</p>
<pre><code>NSLog(@&quot;%@ = %@&quot;, key, obj);
</code></pre><p> }<br> 4). 数据存储原理</p>
<pre><code>a. 当往字典数组中存储一个键值对的时候，这个键值对应该存储在下标为几的元素中
    -&gt;&gt; 并不是按照顺序挨个的存储的
    -&gt;&gt; 存键值对的时候，会根据键值数组的长度做一个哈希算法，算出一个下标，将这个键值存储在该下标处
b. 取值
    也是根据键做一个哈希算法，可以算出键值对存储的下标,然后直接找到这个下标的数据取出就可以了.
</code></pre><p> 5). 与NSArray对比</p>
<pre><code>a. NSArray数组的元素，挨个存，按着顺序
b. 存的效率，NSArray要高; 
取的时候，单个取NSDictionary相对要快，全部取，NSArray快。
</code></pre></li>
<li><p>NSMutableDictionary<br> 1). 是NSDictionary的子类<br> 2). 创建可变字典数组</p>
 <figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">NSMutableDictionary *dict1 = [NSMutableDictionary new];</span><br><span class="line">NSMutableDictionary *dict2 = [[NSMutableDictionary alloc] init];</span><br><span class="line">NSMutableDictionary *dict3 = [NSMutableDictionary dictionary];</span><br></pre></td></tr></table></figure>
<p> 这样创建处理的字典长度为0，有意义，可以动态的新增与删除.</p>
 <figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">NSMutableDictionary *dict = [NSMutableDictionary dictionaryWithObjectsAndKeys:@&quot;jack&quot;, @&quot;name&quot;,nil];</span><br></pre></td></tr></table></figure>
<p> 创建的时候并且初始化.</p>
<p> 注意：<br> 不可以使用以下代码创建</p>
 <figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">NSMutableDictionary *dict = @&#123;@&quot;name&quot;:@&quot;rose&quot;,@&quot;age&quot;:@&quot;18&quot;&#125;;</span><br></pre></td></tr></table></figure>
<p> 3). 新增键值对</p>
 <figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">NSMutableDictionary *dict1 = [NSMutableDictionary new];</span><br><span class="line">[dict1 setObject:@&quot;jack&quot; forKey:@&quot;name&quot;];</span><br></pre></td></tr></table></figure>
<p> 如果键名重复，则会覆盖.<br> 4). 删除所有的键值对<br> - (void)removeAllObjects: 删除所有的键值对<br> - (void)removeObjectForKey:(KeyType)aKey;根据键名删除指定值<br> 5). 持久化–保存到本地</p>
 <figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">写到本地</span><br><span class="line">[dict writeToFile:@&quot;/Users/Apple/Desktop/1.plist&quot; atomically: NO];</span><br><span class="line">读到内存</span><br><span class="line">[NSMutableDictionary dictionaryWithContentsOfFile:@&quot;/Users/Apple/Desktop/1.plist&quot;];</span><br></pre></td></tr></table></figure>
</li>
<li><p>集合的内存管理<br> 将对象存储到集合之中，会为这个对象的引用计数器+1,当集合销毁的时候，就会像存储在集合中的所有的对象发送一条release消息.</p>
<p>使用@[]或者@{}创建的集合已经是被autorelease过的了.<br>直接调用对象方法和类方法创建的对象，也是autorelease过的.</p>
<p>在ARC模式下，集合的元素是一个强类型指针.</p>
</li>
</ol>
<p>二、NSFileManager作用: 创建、删除、赋值、拷贝、移动</p>
<ol>
<li><p>NSFileManager是以单例模式创建的</p>
 <figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">// 单例返回</span><br><span class="line">NSFileManager *fileManager = [NSFileManager defaultManager];</span><br></pre></td></tr></table></figure>
</li>
<li><p>判断<br> 1). 判断指定的文件或者文件夹在磁盘上是否真实的存在<br> - (BOOL)fileExistsAtPath:(NSString <em>)path;<br> 2). 判断指定的路径是否真实的存储在我们的磁盘上，并且判断这个路径是一个文件夹还是一个路径<br> - (BOOL)fileExistsAtPath:(NSString </em>)path isDirectory:(BOOL *)isDirectory;</p>
<pre><code>返回值: 代表这个路径是否真实存在
参数指针：代表这个路径是否是一个文件夹路径,YES为文件夹路径，NO为文件路径
</code></pre><p> 3). 判断指定的文件夹或者文件是否可以读取<br> - (BOOL)isReadableFileAtPath:(NSString <em>)path;<br> 4). 判断指定的文件夹或者文件是否可以写入.<br> - (BOOL)isWritableFileAtPath:(NSString </em>)path;<br> 5). 判断指定的文件夹或者文件是否可以删除<br> - (BOOL)isDeletableFileAtPath:(NSString *)path;</p>
</li>
<li><p>获取信息<br> 1). 获取指定文件或者文件夹的属性信息<br> - (NSDictionary <em>)attributesOfItemAtPath:(NSString </em>)path error:(NSError <strong>)error<br> 返回是一个字典，如果要拿到特定的信息，通过key<br> 2). 获取指定目录下的所有文件的目录，是拿到指定目录下的所有文件和目录，所有的后代目录和文件<br> - (NSArray <em>)subpathsAtPath:(NSString </em>)path;<br> 3). 获取知道给你目录下的所有的子目录和文件，不保护孙子辈<br> - (NSArray <em>)contentsOfDirectoryAtPath:(NSString </em>)path error:(NSError </strong>)error</p>
</li>
<li><p>文件/目录<br> 1). 创建<br> - (BOOL)createFileAtPath:(NSString <em>)path contents:(NSData </em>)data attributes:(NSDictionary *)attr;<br> 第1个参数: 要创建的文件路径<br> 第2个参数: 文件的内容，NSData将别的数据转换为二进制数据</p>
<pre><code>将字符串转换到NSData二进制的方式，调用字符串对象的
- (NSData *)dataUsingEncoding:(NSStringEncoding)encoding;
编码参数:NSUTF8StringEncoding
指定一个编码就可以将字符串转换为二进制数据，存储在NSData对象之中，最后再将这个二进制对象通过这个方法写入，如果想创建一个空文件，第2个参数就给nil。
</code></pre><p> 第3个参数:指定创建的文件的属性，如果想要使用系统的默认值使用nil<br> 2). 在指定的目录创建文件夹<br> - (BOOL)createDirectoryAtPath:(NSString <em>)path withIntermediateDirectories:(BOOL) createIntermediates attributes:(NSDictionary </em>)attributes error:(NSError **)error;<br> 第1个参数：路径<br> 第2个参数：YES，指一路创建，NO不做一路创建<br> 第3个参数：指定属性，nil为系统默认<br> 第4个参数：异常信息</p>
<p> 3). 拷贝文件<br> - (BOOL) copyItemAtPath:(NSString <em>)srcPath toPath:(NSString </em>)dstPath error:(NSError <strong>)error;<br> 4). 移动文件，剪切，重命名<br> - (BOOL) moveItemAtPath:(NSString <em>)srcPath toPath:(NSString </em>)dstPath error:(NSError </strong>)error;<br> 5). 删除文件<br> - (BOOL) removeItemAtPath:(NSString *)path error:(NSError **)error;</p>
<p> 注意: 删除的文件不会倒废纸篓，而是直接删除，小心使用</p>
</li>
</ol>
<p>三、常用的结构体</p>
<ol>
<li><p>CGPoint<br> 1). Foundation框架中CGPoint定义</p>
 <figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">struct CGPoint&#123;</span><br><span class="line">	CGFloat x;</span><br><span class="line">	CGFloat y;</span><br><span class="line">&#125;;</span><br><span class="line">typedef struct CGPoint CGPoint;</span><br></pre></td></tr></table></figure>
<p> typedef CGPoint NSPoint;<br> CGPoint类型实际上就是一个double类型.<br> 这个结构体一般情况下来表示坐标，用来表示控件上的位置</p>
<p> 声明CGPoint变量并初始化的方式:</p>
<pre><code>a. CGPoint p1;
    p1.x = 20;
    p1.y = 30;
b. CGPoint p1 = {20, 30};
c. CGPoint p1 = {.x=20,.y=30};
d. Foundation框架中提供的函数快速创建CGPoint
    a). CGPointMake(x,y)
        CGPoint p1 = CGPointMake(20,30);
    b). NSMakePoint(x,y)
        CGPoint p1 = NSMakePoint(20,30);
</code></pre></li>
<li><p>CGSize<br> 1). 用来描述控件的大小</p>
 <figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">struct CGSize&#123;</span><br><span class="line">	CGFloat width;</span><br><span class="line">	CGFloat height;</span><br><span class="line">&#125;;</span><br><span class="line">typedef struct CGSize CGSize;</span><br></pre></td></tr></table></figure>
<p> typedef CGSize NSSize;<br> CGSize结构体一般用来表示控件的大小</p>
</li>
</ol>
<pre><code>CGSize声明并初始化的方式
    a. CGSize size;
        size.width = 100;
        size.height = 30;
    b. CGSize size = {100, 30};
    c. CGSize size = {.width = 100, .height = 30};
    d. Foundation框架中提供的函数快速创建CGSize
        a). CGSizeMake(x,y)
            CGSize s1 = CGSizeMake(20,30);
        b). NSSizePoint(x,y)
            NSSize s1 = NSMakeSize(20,30);
</code></pre><ol start="3">
<li><p>CGRect<br> 1). 用来描述控件的大小</p>
 <figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">struct CGRect&#123;</span><br><span class="line">	CGPoint origin;</span><br><span class="line">	CGSize size;</span><br><span class="line">&#125;;</span><br><span class="line">typedef struct CGRect CGRect;</span><br></pre></td></tr></table></figure>
<p> typedef CGRect CGRect;<br> CGRect结构体一般用来表示控件的大小</p>
</li>
</ol>
<pre><code>CGRect声明并初始化的方式
    a. CGRect rect;
        rect.origin.x = 20;
        rect.origin.y = 30;
        rect.size.width = 100;
        rect.size.height = 30;
    b. CGRect rect;
        rect.origin = (CGPoint){100, 30};
        rect.size = (CGSize){100, 30};
    d. Foundation框架中提供的函数快速创建CGRect
        a). CGRectMake(x,y,width,height)
            CGRect s1 = CGRectMake(10,20,20,30);
        b). NSRectMake(x,y,width,height)
            NSReca s1 = NSRectMake(10,20,20,30);
</code></pre><p>建议使用CG开头的</p>
<p>四、NSValue</p>
<ol>
<li>NSRange/CGPoint/CGSize/CGRect都是结构体，无法存储到集合中</li>
<li>先将结构体变量存储到OC对象中，在将OC对象存储到集合中</li>
<li>NSValue类的对象就是用来包装结构体变量的<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">CGPoint p1 = CGPointMake(10,20);</span><br><span class="line">NSValue *v1 = [NSValue valueWithPoint:p1];</span><br></pre></td></tr></table></figure>
</li>
</ol>
<p>五、NSDate 时间处理</p>
<ol>
<li><p>创建NSDate时间<br> NSDate <em>date = [NSDate new];<br> NSDate </em>date = [NSDate date];</p>
</li>
<li><p>格式化输出日期<br> yyyy 代表年份<br> MM      代表月份<br> dd   代表日<br> HH   代表24小时制，hh代表12小时制<br> mm   代表分钟<br> ss   代表秒</p>
 <figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line">NSDate *date = [NSDate new];</span><br><span class="line">// 1. 先创建日期格式化对象，输出一个指定格式的对象</span><br><span class="line">NSDateFormatter *formatter = [NSDateFormatter new];</span><br><span class="line">// 2. 指定日期格式化对象 转换的格式</span><br><span class="line">formatter.dateFormat = @&quot;yyyy-MM-dd HH:mm:ss&quot;;</span><br><span class="line">// 3. 格式化对象按照指定的格式将日期对象转换,转换的时候会自动的转换为当前系统的时区</span><br><span class="line">NSString *str = [formatter stringFromDate:date];</span><br><span class="line"></span><br><span class="line">NSString *strDate = @&quot;2011-12-12 12:12:12&quot;;</span><br><span class="line">// 1. 先创建一个日期格式化器对象</span><br><span class="line">NSDateFormatter *formatter = [NSDateFormatter new];</span><br><span class="line">// 2. 指定字符串日期的格式</span><br><span class="line">formatter.dateFormat = @&quot;yyyy-MM-dd HH:mm:ss&quot;;</span><br><span class="line">// 3. 转换</span><br><span class="line">NSDate *date = [formatter dateFromString:strDate];</span><br></pre></td></tr></table></figure>
</li>
<li><p>计算时间<br> 1). 当前时间之上加上一个时间数 </p>
<pre><code>+ (instancetype)dateWithTimeIntervalSinceNow:(NSTimeInterval)secs;

<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">NSDate *date = [NSDate new];</span><br><span class="line">NSDate *date1 = [NSDate dateWithTimeIntervalSinceNow:60*60*10];</span><br></pre></td></tr></table></figure>
</code></pre><p> 2). 两个时间之差</p>
<pre><code>- (NSTimeInterval)timeIntervalSinceDate:(NSDate *)anotherDate;
</code></pre></li>
<li><p>日历对象</p>
 <figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">NSDate *date = [NSDate date];</span><br><span class="line">// 1. 创建一个日历对象</span><br><span class="line">NSCalendar *calendar = [NSCalendar currentCalendar];</span><br><span class="line">// 2. 让日历对象从日期对象取出日期的各个部分</span><br><span class="line">NSDateComponents *com = [calendar components: NSCalendarUnitYear fromDate:date];</span><br><span class="line">com.year,com.month,com.day</span><br></pre></td></tr></table></figure>
</li>
</ol>
<p>六、copy</p>
<ol>
<li><p>无论是MRC还是ARC下，如果属性的类型是NSString类型的，@property参数使用copy.</p>
</li>
<li><p>copy是个方法<br> 1). 定义在NSObject中，作用:拷贝对象</p>
<pre><code>NSString 调用copy方法，没有产生新对象，而是直接将对象本身的地址返回去，这种拷贝我们叫做浅拷贝.
NSMutableString调用copy对象，产生了一个新对象，这种拷贝叫做深拷贝。拷贝出的对象是一个不可变的字符串对象。
</code></pre><p> 2). mutableCopy拷贝之后是一个NSMutableString，是一个可变对象</p>
</li>
<li><p>字符串拷贝计数器问题<br> 1). 若字符串对象存储在常量区中不允许被回收的，所以引用计数器是一个超大的数，retain和release无效<br> 2). 若字符串存储在对去，这个字符串对象和普通的对象是一样的，引用计数器默认是1.<br> 3). 字符串对象如果是浅拷贝，会将对象的引用计数器加1.</p>
<pre><code>字符串对象如果是深拷贝，原来的对象的引用计数器不变.
</code></pre></li>
<li><p>@property的copy</p>
</li>
<li><p>copy方法的内部调用了另外一个方法copyWithZone方法，这个方法是定义在NSCoping协议中的，因为我们类没有遵守NSCopingWithZone方法，当我们自定义的类调用copy方法就会报错。</p>
<ul>
<li>copyWithZone方法实现<br>–&gt; 如果要做深拷贝，即重新创建一个对象，把对象属性的值复制到新对象中，将新对象返回。<br>–&gt; 如果做浅拷贝，就直接将对象返回即可.</li>
</ul>
</li>
</ol>
<p>七、单例模式<br>    一个类的对象，无论在什么时候，什么地方创建，得到的对象都是同一个.最终都会调用该alloc方法创建.<br>    1). alloc方法的内部其实什么都没有做,只是调用了allocWithZone方法.<br>    2). 实际上真正申请空间，创建对象的事情是allocWithZone方法在做.</p>
<ol>
<li><p>要实现单例模式–重写+allocWithZone方法</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">+ (instancetype)allocWithZone:(struct _NSZone *)zone&#123;</span><br><span class="line">	static id instance = nil;</span><br><span class="line">	if(nil == instance)&#123;</span><br><span class="line">		instance = [super allocWithZone:zone];</span><br><span class="line">	&#125;</span><br><span class="line">	return instance;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
</li>
<li><p>单例规范<br> 如果类是一个单例模式，要求为类提供一个类方法，来返回这个单例对象。</p>
<p> 类方法的名称必须以 shared类名; default类名;</p>
</li>
<li><p>什么时候要把类写成单例的<br> 1). 特点: 一个类的对象，无论在什么时候，什么地方创建，得到的对象都是同一个.最终都会调用该alloc方法创建.存储在单例对象中的数据可以被共享.</p>
</li>
</ol>

      
    </div>
    
    
    

    

    

    

    <footer class="post-footer">
      

      
      
      

      
        <div class="post-nav">
          <div class="post-nav-next post-nav-item">
            
              <a href="/blog/2018/08/06/Object-C/Objective-C-学习第六天/" rel="next" title="Objective-C-学习第六天">
                <i class="fa fa-chevron-left"></i> Objective-C-学习第六天
              </a>
            
          </div>

          <span class="post-nav-divider"></span>

          <div class="post-nav-prev post-nav-item">
            
              <a href="/blog/2018/08/06/Object-C/Objective-C-学习第四天/" rel="prev" title="Objective-C-学习第四天">
                Objective-C-学习第四天 <i class="fa fa-chevron-right"></i>
              </a>
            
          </div>
        </div>
      

      
      
    </footer>
  </div>
  
  
  
  </article>



    <div class="post-spread">
      
    </div>
  </div>


          </div>
          


          

  



        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    
    <div class="sidebar-inner">

      

      

      <section class="site-overview-wrap sidebar-panel sidebar-panel-active">
        <div class="site-overview">
          <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
            
              <img class="site-author-image" itemprop="image"
                src="/blog/images/avatar.jpeg"
                alt="凌浩雨" />
            
              <p class="site-author-name" itemprop="name">凌浩雨</p>
              <p class="site-description motion-element" itemprop="description">专业从事于移动端开发，Android与iOS均有涉及，主做Android开发与React Native开发</p>
          </div>

          <nav class="site-state motion-element">

            
              <div class="site-state-item site-state-posts">
              
                <a href="/blog/archives/">
              
                  <span class="site-state-item-count">393</span>
                  <span class="site-state-item-name">posts</span>
                </a>
              </div>
            

            

            

          </nav>

          

          

          
          

          
          

          

        </div>
      </section>

      

      

    </div>
  </aside>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright">&copy; <span itemprop="copyrightYear">2018</span>
  <span class="with-love">
    <i class="fa fa-user"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">凌浩雨</span>

  
</div>


  <div class="powered-by">Powered by <a class="theme-link" target="_blank" href="https://hexo.io">Hexo</a></div>



  <span class="post-meta-divider">|</span>



  <div class="theme-info">Theme &mdash; <a class="theme-link" target="_blank" href="https://github.com/iissnan/hexo-theme-next">NexT.Pisces</a> v5.1.4</div>




        







        
      </div>
    </footer>

    
      <div class="back-to-top">
        <i class="fa fa-arrow-up"></i>
        
      </div>
    

    

  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>









  












  
  
    <script type="text/javascript" src="/blog/lib/jquery/index.js?v=2.1.3"></script>
  

  
  
    <script type="text/javascript" src="/blog/lib/fastclick/lib/fastclick.min.js?v=1.0.6"></script>
  

  
  
    <script type="text/javascript" src="/blog/lib/jquery_lazyload/jquery.lazyload.js?v=1.9.7"></script>
  

  
  
    <script type="text/javascript" src="/blog/lib/velocity/velocity.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/blog/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/blog/lib/fancybox/source/jquery.fancybox.pack.js?v=2.1.5"></script>
  


  


  <script type="text/javascript" src="/blog/js/src/utils.js?v=5.1.4"></script>

  <script type="text/javascript" src="/blog/js/src/motion.js?v=5.1.4"></script>



  
  


  <script type="text/javascript" src="/blog/js/src/affix.js?v=5.1.4"></script>

  <script type="text/javascript" src="/blog/js/src/schemes/pisces.js?v=5.1.4"></script>



  
  <script type="text/javascript" src="/blog/js/src/scrollspy.js?v=5.1.4"></script>
<script type="text/javascript" src="/blog/js/src/post-details.js?v=5.1.4"></script>



  


  <script type="text/javascript" src="/blog/js/src/bootstrap.js?v=5.1.4"></script>



  


  




	





  





  












  





  

  

  

  
  

  

  

  

</body>
</html>
